$(function () {

    //初始化编辑框
    initSNU({
        id:'article-articleContext',
        type:1,
        toolsArr:null,
        height:300,
        gs:true,
        video:false
    });
    //添加资讯
    $('body').on('click','#save',function () {
        saveArticle(1);
    });
    //保存并发布资讯
    $('body').on('click','#saveRelease',function () {
        saveArticle(2);
    });
    //取消操作
    $('body').on('click','#cancel',function () {
        window.history.go(-1);
    });
    //初始化图片上传
    $("body").on('click','#articleImageBtn',function () {//选择图片
        //初始化图片上传窗口
        initUpdateFileWin({
            fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
            callback:'uploadArticleCallback',
            cut:true,//开启剪切
            imgSize:DEFAULT_ARTICLE_IMG_SIZE,//剪切上传后生成图片的规格（开启剪切才起使用）
            aspectRatio:16/9//剪切区域的高宽比例（开启剪切才起使用）
        });
    });
    //删除图片
    $("body").on("click","#remove-image",function () {
        $("#imageJson").val("");
        $("#file-names").text("头像文件名");
        $("#create-time").text("添加时间：");
        $("#article-image").attr("src","");
    }).on('blur',"input[name='clickNum']",function(){//点击量输入监听
        var val = $(this).val();
        if(!/^[0-9]+$/.test(val)){
            $(this).val(0);
        }
    }).on('blur','input[name="keyWord"]',function(){
        var val = $(this).val().replace(/\s*/g,"");
        if(val.length>12){
            icModale(0,"关键字最多为12个字符");
        }
    });

    //保存资讯
    function saveArticle(releaseStatus){
        var parents = $("#article-articleContext").parent('div');
        var tables = parents.find('table');
        if(tables.length>0){
            tables.each(function(index,val){
                $(val).removeAttr("style");
            });
        }
        /*var imgs = parents.find('img');
        if(imgs.length>0){
            imgs.each(function(index,val){
                $(val).removeAttr("style");
            });
        }*/
        var params = $("#commentForm").serializeJSON();
        if(params.keyWord.replace(/\s*/g,"").length>12){
            icModale(0,"关键字最多为12个字符");
            return;
        }
        params.releaseStatus = releaseStatus;
        params.context =$('#article-articleContext').summernote('code');
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction + "/admin/op/article/saveUpdateArticle",
            data:params,
            type:"post",
            dataType:"json",
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                if(result.success == true){
                    icModale(0, result.message);
                    window.location.href = baseloaction + "/admin/op/article/queryArticleList";
                }else{
                    icModale(0,result.message);
                }
            }
        });
    }
});
//图片上传回调
function uploadArticleCallback(result) {
    if(result!=null){
        $("#imageJson").val(encodeURIComponent(JSON.stringify(result)));
        $("#article-image").attr("src",result.pcUrlMap.large);
        $("#file-names").text(result.fileName);
        $("#create-time").text(result.createTime);
    }
}
